﻿using ConfigHelper.Common;
using DataAccessLayer.BaseInterface;
using IDataAccessLayer;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer
{
    public class TeacherDataAccessLayer : BaseDataAccessLayer<Teacher>, ITeacherDataAccessLayer
    {
        //构造函数
        Dictionary<string, bool> usersid = new Dictionary<string, bool>();
        public TeacherDataAccessLayer()
        {
            usersid.Add("Id", false);
        }

        public override string TableName
        {
            get { return "Teacher"; }
        }
        public override Dictionary<string, bool> PKname
        {
            get
            {
                Dictionary<string, bool> dic = new Dictionary<string, bool>();
                dic.Add("TeacherId", true);
                return dic;
            }
        }

        #region 老师信息批量导入
        public bool TeacherAdd(DataTable teacher)
        {            
            StringBuilder TeacherSql = new StringBuilder();
            StringBuilder UserSql = new StringBuilder();
            foreach (DataRow item in teacher.Rows)
            {
                //生成guid
                Guid userid = Guid.NewGuid();

                Teacher t = new Teacher();
                t.UserId = userid;
                t.TeacherName = item[1].ToString();
                t.Sex = item[2].ToString();
                t.Age = Convert.ToInt32(item[3]);
                t.Email = item[4].ToString();
                t.Education = item[5].ToString();
                t.PhoneNumber = item[6].ToString();
                t.Address = item[7].ToString();
                t.CollegeId=Convert.ToInt32(item[8]);
                t.ProfessionalId = Convert.ToInt32(item[9]);
                t.StageId = Convert.ToInt32(item[10]);
                t.SubjectId = Convert.ToInt32(item[11]);
                t.CreateTime = DateTime.Now;                

                Users u = new Users();
                u.Id = userid;
                u.UserName = item[12].ToString();
                u.Password = item[13].ToString();
                u.RoleId = Convert.ToInt32(item[14]);
                u.CreateTime = DateTime.Now;

                //调用common 中拼好的Sql语句
                TeacherSql.Append(Common.AddSQL<Teacher>(t, TableName, PKname));

                //用户表中的id是否是自增

                UserSql.Append(Common.AddSQL<Users>(u, "Users", usersid));
            }
            return DBhelper.UsersTeacherAdd(UserSql.ToString(),TeacherSql.ToString());
        }
        #endregion
    }
}
